"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : dsscmd工具读取磁阵文件内容之-p path参数验证
Create At   : 2023/4/12
Owner       : opentestcase019
Description :
    1.读取磁阵文件内容，指定-p path参数为有效值
    2.读取磁阵文件内容，指定-p path参数为不存在的路径
    3.读取磁阵文件内容，指定-p path参数为包含特殊字符的路径
    4.读取磁阵文件内容，指定-p path参数为权限不足的路径
    5.恢复磁阵文件路径权限，再次读取
    6.在集群停止的情况下执行
    7.在dss关闭的情况下执行
Expect      :
    1.读取成功
    2.合理报错
    3.合理报错
    4.合理报错
    5.读取成功
    6.读取成功
    7.读取成功
History     :
"""
import os
import time
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.primary_root = Node('PrimaryRoot')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.constant = Constant()

    def test_sharestorage_tool(self):
        step1 = "-----step1:读取磁阵文件内容，指定-p path参数为有效值;" \
                "expect:读取成功-----"
        self.logger.info(step1)
        self.logger.info("-----获取磁阵文件-----")
        shell_cmd = f"source {macro.DB_ENV_PATH};" \
                    f"cat {macro.DSS_INSTANCE}/cfg/dss_vg_conf.ini"
        self.logger.info(shell_cmd)
        shell_msg = self.common.get_sh_result(self.primary_node, shell_cmd)
        file1 = shell_msg.splitlines()[0].split(':')[-1].strip()
        file2 = shell_msg.splitlines()[1].split(':')[-1].strip()
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd dev -p {file1} -o 512 -f c;" \
            f"dsscmd dev -p {file2} -o 2048 -f c"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"filename is {file1}, offset is 512.",
                      execute_msg, "执行失败" + step1)
        self.assertIn(f"filename is {file2}, offset is 2048.",
                      execute_msg, "执行失败" + step1)

        step2 = "-----step2:读取磁阵文件内容，指定-p path参数为不存在的路径;" \
                "expect:合理报错-----"
        self.logger.info(step2)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd dev -p /dev/disk_not_exists -o 512 -f c;"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"Failed to open file /dev/disk_not_exists.",
                      execute_msg, "执行失败" + step2)

        step3 = "-----step3:读取磁阵文件内容，指定-p path参数为包含特殊字符的路径;" \
                "expect:合理报错-----"
        self.logger.info(step3)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd dev -p {file1}@ -o 1024 -f c;" \
            f"dsscmd dev -p {file2}# -o 1024 -f c"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"cmd dev error:2193 Path {file1}@ decode error , "
                      f"path should be [0~9,a~z,A~Z,-,_,/,.].",
                      execute_msg, "执行失败" + step3)
        self.assertIn(f"cmd dev error:2193 Path {file2}# decode error , "
                      f"path should be [0~9,a~z,A~Z,-,_,/,.].",
                      execute_msg, "执行失败" + step3)

        step4 = "-----step4:读取磁阵文件内容，指定-p path参数为权限不足的路径;" \
                "expect:合理报错-----"
        self.logger.info(step4)
        shell_cmd = f"ls -l {file1};"
        self.logger.info(shell_cmd)
        shell_msg = self.common.get_sh_result(self.primary_root, shell_cmd)
        disk_path = os.path.join('/dev', shell_msg.split('/')[-1].strip())
        shell_cmd = f"chmod 000 {disk_path}; ls -l {disk_path}"
        self.logger.info(shell_cmd)
        shell_msg = self.common.get_sh_result(self.primary_root, shell_cmd)
        self.assertIn('b---------', shell_msg, "执行失败" + step4)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd dev -p {file1} -o 2048 -f c"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"Failed to open file {file1}.",
                      execute_msg, "执行失败" + step4)

        step5 = "-----step5:恢复磁阵文件路径权限，再次读取;expect:读取成功-----"
        self.logger.info(step5)
        shell_cmd = f"chmod 660 {disk_path}; ls -l {disk_path}"
        self.logger.info(shell_cmd)
        shell_msg = self.common.get_sh_result(self.primary_root, shell_cmd)
        self.assertIn('brw-rw----', shell_msg, "执行失败" + step4)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd dev -p {file1} -o 2048 -f c"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"filename is {file1}, offset is 2048.",
                      execute_msg, "执行失败" + step5)

        step6 = "-----step6:在集群停止的情况下执行;expect:读取成功-----"
        self.logger.info(step6)
        stop_msg = self.primary_sh.exec_cm_ctl(mode='stop')
        self.logger.info(stop_msg)
        self.assertIn(self.constant.cm_stop_success_msg,
                      stop_msg, '执行失败' + step6)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd dev -p {file1} -o 512 -f c;" \
            f"dsscmd dev -p {file2} -o 2048 -f c"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"filename is {file1}, offset is 512.",
                      execute_msg, "执行失败" + step6)
        self.assertIn(f"filename is {file2}, offset is 2048.",
                      execute_msg, "执行失败" + step6)
        start_msg = self.primary_sh.exec_cm_ctl(mode='start')
        self.logger.info(start_msg)
        self.assertIn(self.constant.cm_start_success_msg,
                      start_msg, '启动集群失败')
        time.sleep(30)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn('cluster_state   : Normal', status, "执行失败" + step6)

        step7 = "-----step7:在dss关闭的情况下执行;expect:读取成功-----"
        self.logger.info(step7)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd stopdss;" \
            f"sleep 2;" \
            f"dsscmd dev -p {file1} -o 512 -f c;" \
            f"dsscmd dev -p {file2} -o 2048 -f c"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"Succeed to stop server.", execute_msg, "执行失败" + step7)
        self.assertIn(f"filename is {file1}, offset is 512.",
                      execute_msg, "执行失败" + step7)
        self.assertIn(f"filename is {file2}, offset is 2048.",
                      execute_msg, "执行失败" + step7)

    def tearDown(self):
        self.logger.info("-----恢复环境-----")
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        if "cluster_state   : Normal" not in status:
            start_msg = self.primary_sh.exec_cm_ctl(mode='start')
            self.logger.info(start_msg)
        time.sleep(20)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn('cluster_state   : Normal', status, '恢复环境失败')
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
